package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{
    private static ArrayList<Student> students = new ArrayList<>();

    static {
        Student stu1 = new Student("heima001","张三","23","1991-1-1");
        Student stu2 = new Student("heima002","李四","24","1990-1-1");

        students.add(stu1);
        students.add(stu2);
    }

    //查找学生
    public Student[] findAllStudent() {
        Student[] stu = new Student[students.size()];
        for (int i = 0; i < students.size(); i++) {
            stu[i] = students.get(i);
        }
            return stu;
    }

    //添加学生
    public boolean addStudent(Student student) {
        students.add(student);
        return true;
    }

    //删除学生
    public void deleteStudentInfo(String deleteId) {
        //拿到删除id对应的索引
        int index = getIndex(deleteId);
        students.remove(index);
    }

    //修改学生信息
    public void updataStudentInfo(Student student,String updataId) {
        //拿到需要修改的id索引
        int index = getIndex(updataId);
        //将索引处的值修改为student
        students.set(index,student);
    }

    //拿到索引
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student != null && student.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
}
